<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/vue.js"></script>
    <script src="/js/axios.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.zhimg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.zhimg.com/element-ui/lib/index.js"></script>
    <script src="/js/auth.js"></script>
    <style>
        .custom-tree-node {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            padding-right: 8px;
        }
    </style>
</head>
<body>
<div id="app">
    <el-row>
        <el-col :lg="24" >
            <el-card class="box-card" >
                <el-row>
                    <el-col>
                        <div style="margin: 20px 0">
                            <el-input v-model="typeName" placeholder="请输入要添加的类型名称" clearable style="width: 200px"></el-input>
                            <el-button type="primary" style="margin-bottom: 10px; border-radius: 0px;" @click="add">添加主节点类型</el-button>
                        </div>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <div style="margin: 20px 0">
                            <el-input v-model="filterText" placeholder="请输入关键字进行搜索" clearable style="width: 200px"></el-input>
                            <el-input v-model="type" placeholder="请输入你要添加的类型名" clearable style="width: 200px; float: right"></el-input>
                        </div>

                        <div style="margin: 20px 0">
                            <el-tree
                                    :data="data"
                                    show-checkbox
                                    node-key="id"
                                    default-expand-all
                                    :expand-on-click-node="false"
                                    class="filter-tree"
                                    :props="defaultProps"
                                    :filter-node-method="filterNode"
                                    ref="tree">
                                  <span class="custom-tree-node" slot-scope="{ node, data }">
                                    <span>{{ node.label }}</span>
                                    <span>
                                      <el-button
                                              type="text"
                                              size="mini"
                                              @click="() => append(data)">
                                        添加
                                      </el-button>
                                      <el-button
                                              type="text"
                                              size="mini"
                                              @click="() => remove(node, data)">
                                        删除
                                      </el-button>
                                    </span>
                                  </span>
                            </el-tree>
                        </div>
                    </el-col>
                </el-row>
            </el-card>
        </el-col>
    </el-row>
    <el-dialog></el-dialog>
</div>
<script>
    let p = {headers};
    let id = 1000;
    new Vue({
        el:"#app",
        data:{
            typeName:"",
            pid:'',
            filterText:'',
            type:"",
            data: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
        },
        mounted(){
            axios.get("/exam/qb/menu",p).then(res=>{
                this.data=res.data;
            })
        },
        watch:{
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        methods: {
            handleNodeClick(data) {
                this.pid=data.id;
            },
            filterNode(value, data) {
                    if (!value) return true;
                return data.label.indexOf(value) !== -1;
            },
            add(){
                let param = new URLSearchParams();
                param.append("label",this.typeName);
                axios.post("/exam/qb/add",param,p).then(res=>{
                    if(res.data.code==200){
                        alert("success");
                    }
                })
            },
            append(data) {
                if(this.type.trim()!=''){
                    const newChild = { id: id++, label: this.type, children: [] };
                    if (!data.children) {
                        this.$set(data, 'children', []);
                    }
                    data.children.push(newChild);
                }else {
                    alert("请输入你要添加的节点后，点击父节点的添加按钮")
                }
            },
            remove(node, data) {
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                children.splice(index, 1);
            }
        }
    })
</script>
</body>
</html>